const http = require('http')
const requestConfig = {
  hostname: 'www.baidu.com',
  port: 80,
  path: '/',
  method: 'GET'
}
const request = http.get(requestConfig)
request.on('response', (res) => {
  console.log('状态码:', res.statusCode);
  console.log('响应头', JSON.stringify(res.headers));
  res.setEncoding('utf8')
  res.on('data', (dunk) => {
    console.log(dunk);
  })
})
request.on('socket', (socket) => {
  socket.setTimeout(1000) //socket 超时
  socket.on('timeout', () => {
  })
  console.log();
})
request.setTimeout(1000, () => {
  request.abort()
})
request.on('error', (err) => {
  console.log('请求错误', err);
})